﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsPhoneCloud.Phone.Pages
{
    using System;
    using System.Windows;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel;
    using Microsoft.Samples.WindowsPhoneCloud.StorageClient;

    public partial class QueueDetailsPage : PhoneApplicationPage
    {
        public QueueDetailsPage()
        {
            this.InitializeComponent();

            this.ViewModel = new QueueDetailsPageViewModel();
        }

        public QueueDetailsPageViewModel ViewModel
        {
            get { return this.DataContext as QueueDetailsPageViewModel; }
            set { this.DataContext = value; }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.ViewModel.Queue = GetApplicationState<ICloudQueue>("CurrentQueue");
            if (this.ViewModel.Queue == null)
            {
                // If there is not any queue in the application state, then go back.
                this.NavigationService.GoBack();
            }
            else
            {
                RemoveApplicationState("CurrentQueue");
            }
        }

        private static T GetApplicationState<T>(string key)
        {
            if (!PhoneApplicationService.Current.State.ContainsKey(key))
            {
                return default(T);
            }

            return (T)PhoneApplicationService.Current.State[key];
        }

        private static void RemoveApplicationState(string key)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(key))
            {
                PhoneApplicationService.Current.State.Remove(key);
            }
        }

        private void OnQueueMessage(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                if (string.IsNullOrEmpty(this.ViewModel.QueueMessageContent))
                {
                    MessageBox.Show("The queue message cannot be null nor empty.", "Queue Message Result", MessageBoxButton.OK);
                }
                else
                {
                    this.ViewModel.QueueMessage();
                }
            }
        }

        private void OnDequeueMessage(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.DequeueMessage();
            }
        }

        private void OnDeleteQueueMessages(object sender, EventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.CloudQueueMessages.Clear();
            }
        }
    }
}